#include "VPlayer.h"
#include "VButton.h"
#include "VMain.h"
#ifdef UNDER_CE
VPlayer* VPlayer::s_pThis = NULL;
VPlayer::event_t VPlayer::s_event = NOEVENT;


VPlayer::VPlayer(void) :
    _currentSong(0),
    _songlist(new songlist_t()),
    _playStatus(false),
    _silentStatus(false),
    _eventargument()
{
    setCallback(
        reinterpret_cast<thread_callback_t>(
            &VPlayer::vplayerController
        ));

    s_pThis = this;



    initplayer(
        1, 1, 420, 300,
        false,   // VISIBLE: no effect.
        /*reinterpret_cast<void(*)(int)>(*/
        &VPlayer::playDone/*)*/);

    start();
}

VPlayer::~VPlayer(void)
{
}

void VPlayer::nextsong()
{

#pragma warning (disable: 4018)

    if (_currentSong >= _songlist->size() - 1)
#pragma warning (default: 4018)
    {
        _currentSong = 0;
    }
    else
    {
        ++_currentSong;
    }

    wchar_t buf[512] = {0};
    VMain::ansi2Unicode(
        _songlist->at(_currentSong).path.c_str(), buf);

    NextSong(buf);

    _playStatus = true;
    _silentStatus = false;
}

void VPlayer::addSong(const std::string& songname, const std::string& path)
{
    songlist_t::reverse_iterator it = _songlist->rbegin();
    song_t song;

    if (_songlist->empty())
    {
        song.id = 0;
    }
    else
    {
        song.id = it->id + 1;
    }

    song.songname = songname;
    song.path = path;

    _songlist->push_back(song);
}

void VPlayer::setSilentMode()
{
    SetAudioVolume(0, 0);
}

void VPlayer::setNormalMode()
{
    if (!_started)
    {
        start(NULL);
    }

    SetAudioVolume(15, 0);
}

void VPlayer::play()
{

    if (!_playStatus)
    {
        PauseOrContinue(PLAY);

        //if (GetPlayingStatus() == PLAY_STATUS_PLAY)
        {
            _playStatus = true;
        }
    }
}

void VPlayer::pause()
{

    if (_playStatus)
    {
        PauseOrContinue(PAUSE);

        //if (GetPlayingStatus() == PLAY_STATUS_PAUSE)
        {
            _playStatus = false;
        }
    }
}

void VPlayer::eventControl(event_t event)
{
    if (event == NOEVENT)
    {
        return;
    }

    switch (event)
    {
    case NEXT:
        nextsong();
        break;
    case PLAY:
        play();
        break;
    case PAUSE:
        pause();
        break;
    case SILENT:
        setSilentMode();
        break;
    case NORMAL:
        setNormalMode();
        break;
    default:
        ;
    }
}


int SDLCALL VPlayer::vplayerController(void*)
{
    event_t event;


    if (s_pThis == NULL)
    {
        createVPlayer();
    }


    for (; ;)
    {
        if (!getEvent(&event))
        {
            SDL_Delay(10);

            continue;
        }

        s_pThis->eventControl(event);
        SDL_Delay(10);
    }

    return EXIT_SUCCESS;
}

VPlayer* VPlayer::createVPlayer()
{
    if (s_pThis != NULL)
    {
        return s_pThis;
    }
    else
    {
        s_pThis = new VPlayer;
        return s_pThis;
    }
}

void VPlayer::playDone(int)
{
    cbNext(0, 0, NULL);
}

bool VPlayer::getEvent(event_t* event)
{
    if (s_event != NOEVENT)
    {
        *event = s_event;

        s_event = NOEVENT;

        return true;
    }

    s_event = NOEVENT;
    return false;
}

bool CALLBACK VPlayer::cbPlayOrPause(int, int, VButton* sender)
{
    if (sender->status() == true) // == PLAY
    {
        s_event = PLAY;
    }
    else
    {
        s_event = PAUSE;
    }

    return true;
}

bool CALLBACK VPlayer::cbNext(int, int, VButton*)
{
    DEBUG_OUTPUT("debug %d", 3);
    s_event = NEXT;
    return true;
}

bool CALLBACK VPlayer::cbSilentOrNormal(int, int, VButton* sender)
{
    if (sender->status() == true) // == SILENT
    {
        s_event = SILENT;
    }
    else
    {
        s_event = NORMAL;
    }

    return true;
}



#endif  // UNDER_CE